<?php

// Do not modify. Just use the MVC!

/**
 * \mainpage rpd Framework
 * 
 * 
 * \section intro_sec Introduction
 * 
 * Thank you for using rpd Framework.
 * 
 * \section content Related Pages
 * - \subpage Installation
 * - \subpage first_steps
 * - \subpage folders
 * - \subpage hook
 */

/**
 * \page Installation
 * \section install_sec Installation
 * \subsection step1 Step 1: Download your copy of rpd
 * 
 * Visit http://code.google.com/p/rpd/downloads and download the latest version
 * of rpd.
 * 
 * \subsection step2 Step 2: Upload rpd
 * 
 * Connect to your FTP and upload the whole files where you want.
 * 
 * \subsection step3 Step 3: Done
 * 
 * Thank you for using rpd Framework. We hope you enjoy it an write new
 * applications faster than ever.
 */

/**
 * \page first_steps First steps
 * \section first_steps First steps
 * MVC
 * 
 * \page folders Structure of folders
 * \section folders Structure of folders
 * 
 * - Controler (used for MVC)
 *   - define the action for new sites here
 * - Hook (collects different additions)
 *   - Config (configure rpd here)
 *   - Plugin (add plugins here)
 * - Model (used for MVC)
 *   - define new models for getting data here
 * - Rpd (the framework itself)
 * - View (used for MVC)
 *   - define the design for new sites here, where index.php goes as first page
 */

/**
 * \page hook Hook (Additional files)
 * 
 * \section intro Introduction
 * 
 * The Hook is ment to manipulate the given framework for implementing new
 * plugins or configure the system.
 * 
 * \section Properties
 * 
 * \subsection config $config - the main configuration
 * 
 * \subsection rpd $rpd - exchange classes
 * 
 * If you want to replace the current used classes (e.g. "View" or "Db") with
 * another one defined in a different php for example just go like this:
 * 
 * @code
 * // replacing View
 * Hook::$rpd["View"] = "Hook/plugins/My_View_Plugin/View.php";
 * 
 * // replacing Db
 * Hook::$rpd["Db"] = "Hook/plugins/Path_to_it/The_Filename.php";
 * @endcode
 * 
 * \subsection plugin $plugin - import plugins
 */

require_once('rpd/Hook.php');

function __autoload($class)
{
	$file = str_replace('_', '/', $class) . '.php';
	
	if (isset(Hook::$classes[$class]))
	{
		require_once Hook::$classes[$class];
	}
	else if (file_exists('rpd/' . $file))
	{
		require_once 'rpd/' . $file;
	}
	else
	{
		require_once $file;
	}
}

require_once 'bootstrap.php';
require_once 'Hook/Config/db.php';
require_once 'Hook/Config/plugin.php';

if (!empty(Hook::$config["env"]["root"]))
{
	$_SERVER['REDIRECT_URL'] = str_replace(Hook::$config["env"]["root"],
	                                       '',
	                                       $_SERVER['REDIRECT_URL']);
}

//foreach ($hook->config->plugin as $o)
//{
//	$o->startup();
//}

if (!isset($_SERVER['REDIRECT_URL']) || $_SERVER['REDIRECT_URL'] == '/')
{
	$_SERVER['REDIRECT_URL'] = '/index';
}
else
{
	while (
		   !file_exists('View' . $_SERVER['REDIRECT_URL'] . '.php')
		&& !file_exists('Controller' . $_SERVER['REDIRECT_URL'] . '.php')
		&& $_SERVER['REDIRECT_URL'] != '')
	{
		$val = strrchr($_SERVER['REDIRECT_URL'], '/');
		$_SERVER['REDIRECT_URL'] = substr($_SERVER['REDIRECT_URL'], 0, -strlen($val));

		$var = strrchr($_SERVER['REDIRECT_URL'], '/');
		$_SERVER['REDIRECT_URL'] = substr($_SERVER['REDIRECT_URL'], 0, -strlen($var));

		$_GET[strtolower(substr($var, 1))] = urldecode(substr($val, 1));
	}
}

if ($_SERVER['REDIRECT_URL'] == '')
{
	$_SERVER['REDIRECT_URL'] = '/index';
}

//foreach ($config_plugin as $o)
//{
//	$o->pre();
//}

if (file_exists('Controller' . $_SERVER['REDIRECT_URL'] . '.php'))
{
	require_once 'Controller' . $_SERVER['REDIRECT_URL'] . '.php';
}
else
{
	echo new View($_SERVER['REDIRECT_URL']);
}

//$key = array_keys($config_plugin);
//$i = count($key)-1;
//for ($i; $i >= 0; $i--)
//{
//	$hook->config->plugin[$key[$i]]->post();
//}
//
//$i = count($key)-1;
//for ($i; $i >= 0; $i--)
//{
//	$hook->config->plugin[$key[$i]]->shutdown();
//}
?>